package leetcode;

import java.util.Stack;

public class ques_20 {
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch;
            ch=s.charAt(i);
            if(ch=='('||ch=='['||ch=='{'){
                stack.push(ch);
            }else if(!stack.empty()&&(ch==')'||ch==']'||ch=='}')){
                char top=stack.peek();
                if(top=='['&&ch==']'||top=='('&&ch==')'||top=='{'&&ch=='}'){
                    stack.pop();
                }else{
                    return false;
                }
            }else{
                return false;

            }
        }

        return stack.empty();
    }
}
